"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTotalMap = exports.transferToNumber = exports.createMapData = exports.getTime = exports.initDataLastPaid = exports.initDataFaucet = exports.initDataFive = exports.initDataDaily = exports.initDataHour = void 0;
const dayjs_1 = __importDefault(require("dayjs"));
const uuid_1 = __importDefault(require("uuid"));
//初始化hour
function initDataHour(clients) {
    return __awaiter(this, void 0, void 0, function* () {
        const { now_hour } = getTime();
        let hourArr = new Array();
        const time = new Date().getTime();
        console.log("hour_report添加开始");
        const five_datas = yield clients.fiveClient.getMany({});
        const hour_datas = yield clients.hourClient.getMany({});
        const faucet_datas = yield clients.faucetClient.getMany({});
        const five_map = createTotalMap(five_datas);
        const hour_map = createTotalMap(hour_datas);
        for (const item of faucet_datas) {
            let hour_total = hour_map.get(item._id) || 0;
            let five_total = five_map.get(item._id);
            let total = Number(five_total) - Number(hour_total);
            hourArr.push({
                _id: uuid_1.default.v1(),
                faucet_id: item._id,
                // total:Math.random()*5,
                total: total,
                timezone: String(now_hour)
                // timezone: dayjs(new Date("2021-07-23 17:00:00")).unix()+''
            });
        }
        console.log(`hour_report表添加时间${new Date().getTime() - time}`);
        return hourArr;
    });
}
exports.initDataHour = initDataHour;
//初始化daily_report
function initDataDaily(clients) {
    return __awaiter(this, void 0, void 0, function* () {
        const dailyArr = new Array();
        const { now_day } = getTime();
        const time = new Date().getTime();
        console.log("daily_report添加开始");
        const daily_datas = yield clients.dailyClient.getMany({});
        const five_datas = yield clients.fiveClient.getMany({});
        const faucet_data = yield clients.faucetClient.getMany({});
        const five_map = createTotalMap(five_datas);
        const daily_map = createTotalMap(daily_datas);
        for (const item of faucet_data) {
            let daily_total = daily_map.get(item._id) || 0;
            let five_total = five_map.get(item._id);
            let total = Number(five_total) - Number(daily_total);
            dailyArr.push({
                _id: uuid_1.default.v1(),
                faucet_id: item._id,
                // total:Math.random()*5,
                total: total,
                timezone: String(now_day)
                // timezone:dayjs(new Date("2021-07-23 00:00:00")).unix()+''
            });
        }
        console.log(`daily_report表添加时间${new Date().getTime() - time}`);
        return dailyArr;
    });
}
exports.initDataDaily = initDataDaily;
//初始化five_report
function initDataFive(data, faucet_id, insertTime) {
    const fiveArr = {
        _id: uuid_1.default.v1(),
        faucet_id: faucet_id,
        total: data.paid_today,
        timezone: String(insertTime)
    };
    return fiveArr;
}
exports.initDataFive = initDataFive;
//初始化faucet
function initDataFaucet(faucet_data) {
    const faucet = {
        _id: uuid_1.default.v1(),
        id: faucet_data.id,
        balance: faucet_data.balance,
        name: faucet_data.name,
        currency: faucet_data.currency,
        url: faucet_data.url,
        timezone: faucet_data.creation_date,
        owner_id: faucet_data.owner_id,
        owner_name: faucet_data.owner_name,
        total_users_paid: faucet_data.owner_name,
        health: faucet_data.health,
        active_users: faucet_data.active_users,
        category: faucet_data.category,
    };
    return faucet;
}
exports.initDataFaucet = initDataFaucet;
//初始化last_paid
function initDataLastPaid(lastPaidData, insertTime, last_paid_data_id) {
    const last_paid = {
        _id: uuid_1.default.v1(),
        last_paid_mins: 0,
        faucet_id: last_paid_data_id,
        paid_today: lastPaidData.paid_today,
        operationTime: String(insertTime)
    };
    return last_paid;
}
exports.initDataLastPaid = initDataLastPaid;
function getTime(t) {
    const now = t ? dayjs_1.default(t) : dayjs_1.default();
    let min = now.startOf('minute').add(1, 'minute');
    let last_month = now.startOf('day').subtract(30, 'day');
    let last_day = now.startOf('hour').subtract(1, 'day');
    return {
        now_min: min.unix(),
        now_hour: now.startOf('hour').unix(),
        now_day: now.startOf('day').unix(),
        now_week: now.startOf('day').subtract(7, 'day').unix(),
        last_month: last_month.unix(),
        last_day: last_day.unix()
    };
}
exports.getTime = getTime;
function createMapData(data) {
    let map = new Map();
    for (const item of data) {
        let element = item;
        map.set(element.faucet_id, item);
    }
    return map;
}
exports.createMapData = createMapData;
/**
 * 科学计数法转为普通数字
 * @param inputNumber
 * @returns
 */
function transferToNumber(inputNumber) {
    if (isNaN(inputNumber)) {
        return inputNumber;
    }
    inputNumber = '' + inputNumber;
    inputNumber = parseFloat(inputNumber);
    const eformat = inputNumber.toExponential(); // 转换为标准的科学计数法形式（字符串）
    let pos = eformat.indexOf('e');
    const baseValue = eformat.substr(0, pos);
    const power = eformat.substr(pos + 1);
    let bitNumber = 0;
    pos = baseValue.indexOf('.');
    if (pos === -1) {
        bitNumber = 0 - power;
    }
    else {
        let float = baseValue.substr(pos + 1);
        bitNumber = float.length - power;
    }
    const number = inputNumber.toFixed(Math.max(0, bitNumber));
    return number;
}
exports.transferToNumber = transferToNumber;
function createTotalMap(datas) {
    const map = new Map();
    for (const element of datas) {
        let item = element;
        let total = map.get(item.faucet_id) || 0;
        total += Number(item.total);
        map.set(item.faucet_id, total);
    }
    return map;
}
exports.createTotalMap = createTotalMap;
